﻿
using Commonality.DTO;
using ICarlifeRespository;
using ICarlifeService;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Models;
using Models.CarsGuanLi;
using NPOI.POIFS.Crypt.Dsig;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;

namespace CarlifeService
{
    public class KeHuGuanXi : BaseServer<Client>, IKeHuGuanXi
    {
        private readonly IKeHuRepository db;
        private readonly IUOfWork uOfWork;
        private readonly IClienttypeRepository clienttype;
        private readonly ISocialRelationships  socialRelationships;
        public KeHuGuanXi(IKeHuRepository db, IUOfWork uOfWork, IClienttypeRepository clienttype,ISocialRelationships socialRelationships) : base(db, uOfWork)
        {
            this.db = db;
            this.uOfWork = uOfWork;
            this.clienttype = clienttype;
            this.socialRelationships= socialRelationships;
        }

        public List<Client> Show()
        {
            return db.GetAll();
        }

        public List<ClientDTO> Shows(DateTime? StartTime, DateTime? EndTime, 
            //int size =5,int lendx=1, 
            string? ClientName = "", string? Phone = "", string? IdNumber = ""
            , int ClientTypeId = 0, int RankId = 0, int Progress = 0, string? Isoverdueid = ""
            )
        {
            var list = from x in db.GetAll()
                       join y in clienttype.GetAll()
                       on x.ClientTypeId equals y.Id
                       join a in clienttype.GetAll()
                      on x.RankId equals a.Id
                       join b in clienttype.GetAll()
                     on x.Progress equals b.Id
                       select new ClientDTO
                       {

                           ClientId = x.ClientId,
                           ClientName = x.ClientName,
                           Phone = x.Phone,
                           Id = x.Id,
                           TypeName = y.TypeName,
                           IdNumber = x.IdNumber,
                           ChannelSource = x.ChannelSource,
                           ClientTypeId = x.ClientTypeId,
                           Department = x.Department,
                           DrivingAge = x.DrivingAge,
                           LengthOfStay = x.LengthOfStay,
                           Marriage = x.Marriage,
                           Posts = x.Posts,
                           NumberofOverduesName = a.TypeName,
                           NumberofContractsNamer = b.TypeName,
                           Isoverdueid = x.Isoverdueid,
                           NumberofContracts = x.NumberofContracts,
                           NumberofOverdues = x.NumberofOverdues,
                           RentStartTime = x.RentStartTime,
                           RankId = x.RankId,
                           Progress=x.Progress,

                       };

            if (ClientName != null)
            {


                list = list.Where(x => x.ClientName.Contains(ClientName));
#pragma warning restore CS8602 // 解引用可能出现空引用。
            }
            if (Phone != null)
            {
#pragma warning disable CS8602 // 解引用可能出现空引用。
                list = list.Where(x => x.Phone.Contains(Phone));
#pragma warning restore CS8602 // 解引用可能出现空引用。
            }
            if (IdNumber != null)
            {
#pragma warning disable CS8602 // 解引用可能出现空引用。
                list = list.Where(x => x.IdNumber.Contains(IdNumber));
#pragma warning restore CS8602 // 解引用可能出现空引用。
            }
            if (StartTime != null)
            {
                list = list.Where(x => x.RentStartTime >= Convert.ToDateTime(StartTime));
            }
            if (EndTime != null)
            {
                list = list.Where(x => x.RentStartTime <= (Convert.ToDateTime(EndTime)));
            }
            if (ClientTypeId !=0)
            {
                list = list.Where(x => x.ClientTypeId == ClientTypeId);
            }
            if (RankId != 0)
            {
                list = list.Where(a=> a.RankId == RankId);
            }
            if (Progress != 0)
            {
                list = list.Where(b=> b.Progress == Progress);
            }
            if (!string.IsNullOrEmpty(Isoverdueid))
            {
                list = list.Where(x => x.Isoverdueid == Isoverdueid);
            }
            return list.ToList();
            //return new Page<ClientDTO>
            //{
            //    recordCount = list.Count(),
            //    plist = list.OrderByDescending(a => a.ClientId).Skip((lendx - 1) * size).Take(size).ToList(),
            //};


        }

        public List<ClientDTO> ClientBackfill(int id)
        {

            var list = from b in db.GetAll()
                       join    x in db.GetAll().Where(x => x.Id == id)
                         on b.ClientId equals x.ClientId
                       join y in clienttype.GetAll()
                      on x.ClientTypeId equals y.Id
                       join a in socialRelationships.GetAll()
                      on x.SocialID equals a.Id
                       select new ClientDTO
                       {
                           ClientId = x.ClientId,
                           ClientName = x.ClientName,
                           Phone = x.Phone,
                           Id = b.Id,
                           TypeName = y.TypeName,
                           IdNumber = x.IdNumber,
                           ChannelSource = x.ChannelSource,
                           ClientTypeId = x.ClientTypeId,
                           Department = x.Department,
                           DrivingAge = x.DrivingAge,
                           LengthOfStay = x.LengthOfStay,
                           Marriage = x.Marriage,
                           Posts = x.Posts,
                           Isoverdueid = x.Isoverdueid,
                           NumberofContracts = x.NumberofContracts,
                           NumberofOverdues = x.NumberofOverdues,
                           Sex=x.Sex,
                           ResidentialAddress=x.ResidentialAddress,
                           Workplace=x.Workplace,
                           workingHours = x.workingHours,
                           SocialID=x.SocialID,
                           FamilyMembers =a.FamilyMembers,
                           Relationship = a.Relationship,
                           MonthlyIncome = a.MonthlyIncome,
                           Children = a.Children,
                           Urgent = a.Urgent,
                           SContact = a.SContact,
                           Sidentitycard = a.Sidentitycard,
                           JobTitle = a.JobTitle,
                           Guarantee = a.Guarantee,
                           OtherIncome = a.OtherIncome,
                           TypeOfHouse = a.TypeOfHouse,
                           Description = a.Description,

                       };
            return list.ToList();


        }



    }
}
